﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RedemptoristRetreatCenter.DatabaseUtility;
using RedemptoristRetreatCenter.HelperUtility;

namespace RedemptoristRetreatCenter
{
    /// <summary>
    /// Volunteer class contains all of the functionality behind signing up a volunteer 
    /// </summary>
    public partial class Volunteer : System.Web.UI.Page
    {
        UserAccountInfo curUser = new UserAccountInfo();
        UserAccountUtility userAccountUtil = new UserAccountUtility();
        VolunteerUtility volUtil = new VolunteerUtility();

        /// <summary>
        /// Upon page load, bind the state abbreviations to the drop down menu and if the user is logged in, based on the session find the user's info 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            //StateAbbreviations states = new StateAbbreviations();
            //VolState.DataSource = states.getListOfStates();
            VolState.DataBind();

            if (Session["Username"] != null)
            {
                if(userAccountUtil.userExists((String)Session["Username"]))
                {
                    curUser = userAccountUtil.getUserAccountInfoByUsername((String)Session["Username"]);
                    setVolFields(curUser);
                } 
            }
        }

        /// <summary>
        /// If the user is logged in, populate the fields in the volunteer sign up form
        /// </summary>
        /// <param name="curUser"></param>
        private void setVolFields(UserAccountInfo curUser)
        {
            // -- Populate the volunteer fields with the current user's info --
            VolFirstName.Text = curUser.firstName;
            VolLastName.Text = curUser.lastName;
            VolAddress.Text = curUser.addressLine1;

            if(VolAddressLineTwo.Text != null)
                VolAddressLineTwo.Text = curUser.addressLine2;

            VolCity.Text = curUser.city;
            VolState.Text = curUser.state;
            VolZip.Text = curUser.zipCode;
            VolHomePhone.Text = curUser.homePhone;
            VolEmail.Text = curUser.emailAddress;
        }

        /// <summary>
        /// If the user already has a entry in the database, this function makes sure that the volunteer field is set to true
        /// If the user does not have an entry in the database, this function creates an entry (without a username/password)
        /// </summary>
        protected void AddUserToVolunteerList()
        {
            // -- if the current user exists in the database, update the entry to include the volunteer field as true --
            if (curUser.username != null)
            {
                if (!(curUser.volunteer))
                {
                    curUser.volunteer = true;
                    userAccountUtil.updateUserAccount(curUser, curUser.username);
                }
            }
            // -- if the current user does not have an entry in the database, create the entry --
            else
            {
                curUser.firstName = VolFirstName.Text;
                curUser.lastName = VolLastName.Text;
                curUser.addressLine1 = VolAddress.Text;
                curUser.addressLine2 = VolAddressLineTwo.Text;
                curUser.city = VolCity.Text;
                curUser.state = VolState.Text;
                curUser.zipCode = VolZip.Text;
                curUser.homePhone = VolHomePhone.Text;
                curUser.emailAddress = VolEmail.Text;
                curUser.volunteer = true;
                curUser.username = null;
                curUser.password = null;
                curUser.staff = false;
                curUser.cellPhone = null;
                curUser.businessPhone = null;
                curUser.biographyUrl = null;

                userAccountUtil.insertUserAccount(curUser, null);
                curUser.userId = userAccountUtil.getUserId(curUser.firstName, curUser.lastName, curUser.emailAddress);
            }
        }

        /// <summary>
        /// Upon clicking the volunteer submit button, the user is added to the volunteer list within the database 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void VolSubmit_Click(object sender, EventArgs e)
        {
            AddUserToVolunteerList();

            if (volUtil.AddVolunteerTasks(curUser.userId, collectVolunteerTasks()))
                VolMessage.Text = "Your volunteer sign up was successful";
            else
                VolMessage.Text = "Uh oh, there was a problem signing you up for volunteer tasks";
        }

        /// <summary>
        /// Collects all of the volunteer tasks that the current user selected 
        /// </summary>
        /// <returns>List<Strings></Strings></returns>
        private List<String> collectVolunteerTasks()
        {
            List<String> volunteerTasks = new List<String>();

            if (BulkMailing.Checked == true)
                volunteerTasks.Add("BulkMailing");
            if (Carpentry.Checked == true)
                volunteerTasks.Add("Carpentry");
            if (BushRemoval.Checked == true)
                volunteerTasks.Add("BushRemoval");
            if (ComputerMaintenance.Checked == true)
                volunteerTasks.Add("ComputerMaintenance");
            if (Electrical.Checked == true)
                volunteerTasks.Add("Electrical");
            if (PlantCare.Checked == true)
                volunteerTasks.Add("PlantCare");
            if (DataEntry.Checked == true)
                volunteerTasks.Add("DataEntry");
            if (HVAC.Checked == true)
                volunteerTasks.Add("HVAC");
            if (LeafRaking.Checked == true)
                volunteerTasks.Add("LeafRaking");
            if (GeneralOffice.Checked == true)
                volunteerTasks.Add("GeneralOffice");
            if (Painting.Checked == true)
                volunteerTasks.Add("Painting");
            if (Pruning.Checked == true)
                volunteerTasks.Add("Pruning");
            if (Library.Checked == true)
                volunteerTasks.Add("Library");
            if (Plumbing.Checked == true)
                volunteerTasks.Add("Plumbing");
            if (Weeding.Checked == true)
                volunteerTasks.Add("Weeding");
            if (WordProcessing.Checked == true)
                volunteerTasks.Add("WordProcessing");
            if (Refrigerator.Checked == true)
                volunteerTasks.Add("Refrigerator");
            if (RoofingGutters.Checked == true)
                volunteerTasks.Add("RoofingGutters");
            if (RoadRepair.Checked == true)
                volunteerTasks.Add("RoadRepair");
            if (EngineRepair.Checked == true)
                volunteerTasks.Add("EngineRepair");
            if (WindowWashing.Checked == true)
                volunteerTasks.Add("WindowWashing");

            return volunteerTasks;
        }
    }
}